package com.cbtx.utils

import android.content.Context
import android.net.ConnectivityManager
import android.text.TextUtils
import java.net.Inet4Address
import java.net.NetworkInterface
import java.net.SocketException

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/1 12:48 PM
 * desc   :
 */
object NetWorkUtil {
    /**
     * 检查网络是否可用
     *
     * @param paramContext
     * @return
     */
    fun checkEnable(paramContext: Context): Boolean {
        val localNetworkInfo = (paramContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        return localNetworkInfo != null && localNetworkInfo.isAvailable
    }

    /**
     * 获取当前网络状态
     *
     * @param context
     * @return
     */
    fun getNetWorkState(context: Context): Int {
        val network_none = -1 // 无网络
        val network_mobile = 0 // 移动网络
        val network_wifi = 1 // 无线网络
        val network_eth = 2 // 有线网络

        // 得到连接管理器对象
        val connectivityManager = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
            if (activeNetworkInfo.type == ConnectivityManager.TYPE_WIFI) {
                return network_wifi
            } else if (activeNetworkInfo.type == ConnectivityManager.TYPE_MOBILE) {
                return network_mobile
            } else if (activeNetworkInfo.type == ConnectivityManager.TYPE_ETHERNET) {
                return network_eth
            }
        } else {
            return network_none
        }
        return network_none
    }
}